#pragma once
#include <iostream>
#include <string.h>
#include <cstdio>
#include <sys/types.h>
#include <sys/ipc.h>
#include <stdalign.h>
#include <sys/shm.h>
#include <unistd.h>
const std::string path = "/home/lxp/linux";
int projId = 0X6666;
int gshmsize = 4096;
mode_t gmode = 0600;

class sharememory
{
private:
    void CreateShmHelper(int shmflg)
    {
        _key = ftok(path.c_str(), projId);
        if (_key < 0)
        {
            std::cerr << "ftok error" << std::endl;
            return;
        }
        _shmid = shmget(_key, gshmsize, shmflg);
        if (_shmid < 0)
        {
            std::cerr << "shmget error" << std::endl;
            return;
        }
        std::cout << "shmid: " << _shmid << std::endl;
    }

public:
    sharememory()
        : _shmid(-1), _key(-1), _add(nullptr)
    {
    }
    void CreateShm()
    {
        CreateShmHelper(IPC_CREAT | IPC_EXCL | gmode);
    }
    void Getshm()
    {
        CreateShmHelper(IPC_CREAT);
    }
    void Attach()
    {
        _add = shmat(_shmid, nullptr, 0);
        if ((long long)_add == -1)
        {
            std::cout << "attach error" << std::endl;
        }
    }
    void DetachShm()
    {
        if (_add != nullptr)
        {
            shmdt(_add);
        }
        std::cout << "detach done: " << std::endl;
    }
    void DeletaShm()
    {
        shmctl(_shmid, IPC_RMID, nullptr);
    }
    void *GetAdd()
    {
        return _add;
    }
    ~sharememory()
    {
    }

private:
    int _shmid;
    key_t _key;
    void *_add;
};

sharememory shm;

struct data
{
    char status[32];
    char lasttime[48];
    char image[4000];
};